package com.nvc.homeplan;


import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
import com.nvc.homeplan.data.vo.*;

import java.util.List;

public class PlanListPage extends ListActivity {

    private static final String CLASSTAG = PlanListPage.class.getSimpleName();
    private static final int MENU_CHANGE_CRITERIA = Menu.FIRST + 1;
    private static final int MENU_GET_NEXT_PAGE = Menu.FIRST;
    private static final int NUM_RESULTS_PER_PAGE = 8;
    
    private TextView empty;    
    private ProgressDialog progressDialog;
    //private PlanListAdapter reviewAdapter;
    private List<PlanVO> reviews;
    private HomePlanApplication application;
    
    private final Handler handler = new Handler() {
        @Override
        public void handleMessage(final Message msg) {
            Log.v(Constants.LOGTAG, " " + PlanListPage.CLASSTAG + " worker thread done, setup PlanListAdapter");
        	progressDialog.dismiss();
            if ((reviews == null) || (reviews.size() == 0)) {
                empty.setText("No Data");
            } else {
            	if (application.searchType == "PB")
            	{
            		PlanListAdapterPlanBook reviewAdapter = new PlanListAdapterPlanBook(PlanListPage.this, reviews);
            		setListAdapter(reviewAdapter);
            	}
            	else
            	{
            		PlanListAdapter reviewAdapter = new PlanListAdapter(PlanListPage.this, reviews);
            		setListAdapter(reviewAdapter);
            	}
            }
        }
    };   

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.v(Constants.LOGTAG, " " + PlanListPage.CLASSTAG + " onCreate");
        this.setContentView(R.layout.plan_list_page);

        this.empty = (TextView) findViewById(R.id.empty);

        // set list properties
        final ListView listView = getListView();
        listView.setItemsCanFocus(false);
        listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        listView.setEmptyView(this.empty);
        application = (HomePlanApplication) getApplication();
        
        loadPlans();
    }   

    /*
    @Override
    protected void onResume() {
        super.onResume();
        loadPlans();
    }    
    */
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
    	application.setPlanID(this.reviews.get(position).id);

        Intent intent = new Intent(Constants.INTENT_ACTION_PLAN_DETAIL_PAGE);
        startActivity(intent);
    }    
    
    
    private void loadPlans() 
    {
        this.progressDialog = ProgressDialog.show(this, " Working...", " Retrieving Data", true, false);
        new Thread() {
            @Override
            public void run() {
            	try {
					Thread.sleep(1);
					com.nvc.homeplan.data.Plan plan = new com.nvc.homeplan.data.Plan();
					reviews = plan.getPlans(application.searchType, application.searchUrlQueryString);
				} 
            	catch (InterruptedException e) 
            	{
					e.printStackTrace();
				}
                handler.sendEmptyMessage(0);
            }
        }.start(); 
    }
}
